<?php

App::uses('AppController', 'Controller');

class ProductsController extends AppController {

    public $name = 'Products';

    public function index() {
        $products = $this->__products();
        $this->set(compact('products'));
        $this->Product->updateViews($products);
    }

    public function products() {
        $products = $this->__products();
        $this->set(compact('products'));
    }

    public function search() {
        $products = $this->__products();
        if (count($products) == 1) {
            return $this->redirect(array('controller' => 'products', 'action' => 'view', 'slug' => $products[0]['Product']['slug']));
        }
        $this->set(compact('products'));
    }

    public function view($id = null) {

        $product = $this->Product->find('first', array(
            'recursive' => 2,
            'contain' => array(
                'Category',
                'Brand',
                'ProductsImage'
            ),
            'conditions' => array(
                'Brand.active' => 1,
                'Product.active' => 1,
                'Product.slug' => $id
            )
        ));
        if (empty($product)) {
            return $this->redirect(array('action' => 'index'), 301);
        }

        $this->Product->updateViews($product);

        $this->set(compact('product'));

        $productmods = $this->Product->Productmod->getAllProductMods($product['Product']['id'], $product['Product']['price']);
        $this->set('productmodshtml', $productmods['productmodshtml']);

        $this->set('title_for_layout', $product['Product']['name'] . ' ' . Configure::read('Settings.SHOP_TITLE'));
    }

    private function __products() {
        $this->Prg->encode = true;
        $this->Prg->commonProcess();
        $options = array(
            'limit' => 12,
            'recursive' => -1,
            'contain' => array(
                'Brand',
                'ProductsImage',
            ),
            'order' => array(
                'Product.views' => 'ASC'
            ),
            'conditions' => array_merge(
                    array(
                'Brand.active' => 1,
                'Product.active' => 1,
                    ), $this->{$this->modelClass}->parseCriteria($this->passedArgs)
            ),
        );
        $this->paginate = $options;
        return $this->paginate($this->modelClass);
    }

    public function sitemap() {
        $products = $this->Product->find('all', array(
            'recursive' => -1,
            'contain' => array(
                'Brand'
            ),
            'fields' => array(
                'Product.slug'
            ),
            'conditions' => array(
                'Brand.active' => 1,
                'Product.active' => 1
            ),
            'order' => array(
                'Product.created' => 'DESC'
            ),
        ));
        $this->set(compact('products'));

        $website = Configure::read('Settings.WEBSITE');
        $this->set(compact('website'));

        $this->layout = 'xml';
        $this->response->type('xml');
    }

    /* -------------------------------- Administração ----------------------------------------------------- */

    protected function __list() {
        $this->set('category_id', $this->{$this->modelClass}->Category->find('list'));
        $this->set('brand_id', $this->{$this->modelClass}->Brand->find('list'));
    }

    /**
     * Lista todos os "usuarios".
     *
     * @method        admin_index
     * @version       1.0.0
     * @return void
     */
    public function admin_index() {
        parent::admin_index();
    }

    /**
     * Cadastrar "usuarios"
     *
     * @method        admin_add
     * @version       1.0.0
     * @return void
     */
    public function admin_add() {
        parent::admin_add();
    }

    /**
     * Alterar de acordo com o ID  "usuarios" especifico.
     *
     * @method        admin_edit
     * @version       1.0.0
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        parent::admin_edit($id);
    }

    /**
     * Excluir de acordo com o ID  "usuarios" especifico.
     *
     * @method        admin_delete
     * @version       1.0.0
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        parent::admin_delete($id);
    }

    /**
     * Exportar dados do banco em formato CSV da tabela "usuarios".
     *
     * @method        admin_export_data
     * @version       1.0.0
     * @return void
     */
    public function admin_export() {
        parent::admin_export();
    }

    /**
     * Detalhes do registro.
     *
     * @method        admin_view
     * @version       1.0.0
     * @return void
     */
    public function admin_view($id = null) {
        parent::admin_view($id);
    }

}
